Streaming API এবং Tree Model এর ব্যবহার

Java Technologies - জ্যাকসন (Jackson) - Jackson এর Performance Optimization Techniques
186

Jackson লাইব্রেরি JSON ডেটা প্রসেস করার জন্য দুইটি শক্তিশালী পদ্ধতি প্রদান করে:

  1. Streaming API: লো-লেভেল JSON প্রসেসিংয়ের জন্য।
  2. Tree Model: JSON ডেটা স্ট্রাকচারকে ডাইনামিকভাবে ম্যানিপুলেট করার জন্য।

1. Streaming API

Streaming API কী?

Streaming API JSON ডেটা প্রসেস করার একটি লাইটওয়েট এবং মেমরি-অপ্টিমাইজড পদ্ধতি। এটি লিনিয়ার ফ্যাশনে JSON ডেটা পড়তে (Parse) এবং লিখতে (Write) ব্যবহার হয়।

কেন ব্যবহার করবেন?

  • Performance: মেমরি খরচ কম।
  • Large JSON: বড় JSON ডেটা স্ট্রিম হিসেবে প্রসেস করতে কার্যকর।

1.1 JSON Parsing (Reading)

Streaming API এর JsonParser ব্যবহার করে JSON ডেটা পড়া হয়।

উদাহরণ: JSON Parsing

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import java.io.File;
import java.io.IOException;

public class StreamingAPIReadExample {
    public static void main(String[] args) throws IOException {
        JsonFactory factory = new JsonFactory();
        JsonParser parser = factory.createParser(new File("data.json"));

        while (!parser.isClosed()) {
            JsonToken token = parser.nextToken();

            if (token == null) break;

            if (JsonToken.FIELD_NAME.equals(token)) {
                String fieldName = parser.getCurrentName();
                parser.nextToken(); // Move to the value
                System.out.println(fieldName + ": " + parser.getText());
            }
        }

        parser.close();
    }
}

JSON ইনপুট (data.json):

{
  "name": "John",
  "age": 30,
  "city": "Dhaka"
}

আউটপুট:

name: John
age: 30
city: Dhaka

1.2 JSON Writing

Streaming API এর JsonGenerator ব্যবহার করে JSON লেখা যায়।

উদাহরণ: JSON Writing

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

import java.io.File;
import java.io.IOException;

public class StreamingAPIWriteExample {
    public static void main(String[] args) throws IOException {
        JsonFactory factory = new JsonFactory();
        JsonGenerator generator = factory.createGenerator(new File("output.json"));

        generator.writeStartObject(); // Start JSON Object

        generator.writeStringField("name", "John");
        generator.writeNumberField("age", 30);
        generator.writeStringField("city", "Dhaka");

        generator.writeEndObject(); // End JSON Object
        generator.close();
    }
}

আউটপুট (output.json):

{
  "name": "John",
  "age": 30,
  "city": "Dhaka"
}

2. Tree Model

Tree Model কী?

Tree Model JSON ডেটাকে একটি JsonNode ট্রি হিসেবে লোড করে। এটি ডাইনামিক এবং লো-লেভেল JSON ডেটা ম্যানিপুলেট করতে ব্যবহৃত হয়।

কেন ব্যবহার করবেন?

  • JSON ডেটা পরিবর্তন বা বিশ্লেষণ করতে।
  • কাঠামোবিহীন বা ডাইনামিক JSON হ্যান্ডল করতে।
  • Nested JSON ডেটা ম্যানিপুলেট করতে।

2.1 JSON Parsing (Reading)

Tree Model এর মাধ্যমে JSON ডেটা একটি JsonNode-এ লোড করে পড়া যায়।

উদাহরণ: JSON Parsing

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

public class TreeModelReadExample {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(new File("data.json"));

        String name = rootNode.get("name").asText();
        int age = rootNode.get("age").asInt();
        String city = rootNode.get("city").asText();

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("City: " + city);
    }
}

JSON ইনপুট (data.json):

{
  "name": "John",
  "age": 30,
  "city": "Dhaka"
}

আউটপুট:

Name: John
Age: 30
City: Dhaka

2.2 JSON Writing

Tree Model ব্যবহার করে একটি নতুন JSON তৈরি করা যায়।

উদাহরণ: JSON Writing

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.File;
import java.io.IOException;

public class TreeModelWriteExample {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        // Create a root node
        ObjectNode rootNode = mapper.createObjectNode();
        rootNode.put("name", "John");
        rootNode.put("age", 30);
        rootNode.put("city", "Dhaka");

        // Write to JSON file
        mapper.writeValue(new File("output.json"), rootNode);
    }
}

আউটপুট (output.json):

{
  "name": "John",
  "age": 30,
  "city": "Dhaka"
}

2.3 Nested JSON Manipulation

Tree Model ব্যবহার করে Nested JSON ডেটা ম্যানিপুলেট করা যায়।

উদাহরণ: Nested JSON ম্যানিপুলেশন

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.File;
import java.io.IOException;

public class NestedTreeModelExample {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        // Parse JSON file
        JsonNode rootNode = mapper.readTree(new File("nested.json"));

        // Access nested fields
        JsonNode addressNode = rootNode.get("address");
        String street = addressNode.get("street").asText();
        System.out.println("Street: " + street);

        // Modify nested field
        ((ObjectNode) addressNode).put("city", "Chittagong");
        mapper.writeValue(new File("modified.json"), rootNode);
    }
}

JSON ইনপুট (nested.json):

{
  "name": "John",
  "address": {
    "street": "123 Main St",
    "city": "Dhaka"
  }
}

আউটপুট (modified.json):

{
  "name": "John",
  "address": {
    "street": "123 Main St",
    "city": "Chittagong"
  }
}

3. পার্থক্য: Streaming API vs Tree Model

FeatureStreaming APITree Model
PerformanceHigh (memory-efficient)Medium (uses more memory)
Use CaseLarge or simple JSONComplex or nested JSON
FlexibilityLow (linear parsing)High (easy manipulation)
Dynamic StructureNoYes
Ease of UseModerate (low-level control needed)High (node-based manipulation)

  • Streaming API: বড় JSON ফাইলের জন্য বা মেমরি অপ্টিমাইজেশনের প্রয়োজনে।
  • Tree Model: Nested বা কাঠামোবিহীন JSON ম্যানিপুলেশনের জন্য।
Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...